package com.dwms.controller;

import com.dwms.entity.ListRequest;
import com.dwms.entity.PageResponse;
import com.dwms.entity.QueryRequest;
import com.dwms.entity.Result;
import com.dwms.entity.base.product.request.ProductImportRequest;
import com.dwms.entity.base.product.request.ProductQueryRequest;
import com.dwms.entity.base.product.request.ProductRequest;
import com.dwms.entity.base.product.response.ProductResponse;
import com.dwms.feign.goods.ProductFeignClient;
import com.dwms.utils.EmptyUtils;
import com.dwms.utils.ExcelImportUtil;
import com.dwms.utils.WebUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @ClassName: ProductController
 * @Description: Product
 * @Create 2025-09-22 16:15:43
 * @Author WH
 * @Since 1.0.0
**/
@Tag(name = "商家商品管理")
@RestController
@RequestMapping(value = "/product")
@RequiredArgsConstructor
public class ProductController {

    private final HttpServletRequest request;
    private final ProductFeignClient productFeignClient;

    /**
     * 保存
     *
     * @param productRequest
     *          请求参数
     * @return
     */
    @Operation(summary = "新增商品")
    @PostMapping("/save")
    public Result save(@RequestBody ProductRequest productRequest) {
        WebUtil.getAdminRequest(request,productRequest);
        return productFeignClient.save(productRequest);
    }

    /**
     * 修改
     *
     * @param productRequest
     *          请求参数
     * @return
     */
    @Operation(summary = "修改商品")
    @PostMapping("/update")
    public Result update(@RequestBody ProductRequest productRequest) {
        WebUtil.getAdminRequest(request,productRequest);
        return productFeignClient.update(productRequest);
    }
    

    /**
     * 分页查询
     *
     * @param query
     *          查询参数
     * @return
     */
    @Operation(summary = "分页查询")
    @PostMapping("/findPage")
    public Result<PageResponse<ProductResponse>> findPage(@RequestBody ProductQueryRequest query) {
        WebUtil.getAdminRequest(request,query);
        return productFeignClient.findPage(query);

    }

    /**
     *  根据ID查询
     *
     * @param query
     *          参数
     * @return
     */
    @Operation(summary = "根据ID查询")
    @PostMapping("/findById")
    public Result findById(@RequestBody QueryRequest query){
        WebUtil.getAdminRequest(request,query);
        return this.productFeignClient.findById(query);
    }

    /**
     * 删除
     *
     * @param query
     *          参数
     * @return
     */
    @Operation(summary = "删除商品")
    @PostMapping("/delete")
    public Result delete(@RequestBody QueryRequest query){
        WebUtil.getAdminRequest(request,query);
        return this.productFeignClient.delete(query);
    }

    /**
     * 查询列表
     * @param query
     * @return
     */
    @Operation(summary = "查询列表")
    @PostMapping("/findList")
    public Result<List<ProductResponse>> findList(@RequestBody ProductQueryRequest query) {
        WebUtil.getAdminRequest(request,query);
        return this.productFeignClient.findList(query);
    }

    /**
     * 导入商品
     * @param file
     * @return
     */
    @Operation(summary = "导入商品")
    @PostMapping("/importExcel")
    public Result importExcel(@RequestParam(name = "file") MultipartFile file) {
        try {
            String[] headers = {"序号","商品类型", "商品编码", "国际编码", "平台", "商家", "品牌", "商品名称", "商品英文名称", "商品描述","商品小类","规格型号","包装规格","包装单位","采购单价","销售现价",
                    "商品备案名称","商品备案编码","HS编码","入区计量单位","入区计量折算数量","法定计量单位","法定计量数量","第二计量单位","第二计量数量","进口时间","账册备案料号","运输方式","原产国","贸易国","净含量","单位(g/ml/片或张)","备案规格",
                    "商品净重(kg)","商品毛重(kg)", "商品有效期","保鲜天数","是否赠品（样品）","最大在仓天数","安全库存","长（cm）","宽（cm）","高（cm）","临期天数","是否是包材","备注"};
            String[] fields = {"index", "type", "productCode", "barCode", "platName", "sellerName", "brand", "fullName", "enName", "description","category","spec","packSpec","packUnit","purchasePrice","shopPrice",
                    "itemName","itemNo","hsCode","unit","qty","unit1","qty1","unit2","qty2","importedDate","itemRecordNo","transMode","country","tradeCountry","netContents","gunit","gmodel",
                    "netWeight","grossWeight","validPeriodDays","freshDays","giftFlag","maxInDays","safetyStock","length","width","height","advanceDay","packageFlag","memo"};
            List<ProductImportRequest> productImportRequests = ExcelImportUtil.excelToBeanUnderHead(file.getInputStream(), headers, fields, ProductImportRequest.class);
            if (EmptyUtils.isEmpty(productImportRequests)) {
                return Result.failed("数据读取失败");
            }
            if (productImportRequests.size() > 3000) {
                return Result.failed("导入数据超过3000条,请分批次导入.");
            }
            ListRequest<ProductImportRequest> listRequest = new ListRequest<>();
            listRequest.setList(productImportRequests);
            return this.productFeignClient.importExcel(listRequest);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed("导入商品失败" + e.getMessage());
        }
    }
}
